<?php

namespace addons\qingdongams\controller;

use addons\qingdongams\model\CustomerProduct as CustomerProductModel;
use addons\qingdongams\model\Staff;
use think\Db;
use think\Exception;
/**
 * @desc 操作文档：https://doc.fastadmin.net/qingdongams
 * @desc 软件介绍：https://www.fastadmin.net/store/qingdongams.html
 * @desc 售后微信：qingdong_crm
 */
/**
 * 客户产品
 */
class CustomerProduct extends StaffApi
{
    protected $noNeedLogin = [];
    protected $noNeedRight = [];


    //获取客户产品
    public function getList()
    {
        $limit = input("limit/d", 10);
        $customer_id = input("customer_id", 0);

        $where = [ 'customer_id' => $customer_id];
        $where['create_staff_id']=['in',Staff::getMyStaffIds()];
        $records = CustomerProductModel::where($where)->with([
            'customer',
            'createStaff',
            'product'
        ])->order('id desc')->paginate($limit);

        $this->success('请求成功', $records);
    }

    //获取列表
    public function getSelectList()
    {
        $customer_id = input("customer_id", 0);
        $status = input("status", 0);
        $where = ['customer_id' => $customer_id,'status'=>['neq',9]];
        if($status){
            if($status == 3){
                $where[ 'status'] =['in',[3,4]];
            }else{
                $where[ 'status'] =$status;
            }
        }
        $records = CustomerProductModel::where($where)->with([
            'product'
        ])->field('id,product_id,status,pay_date,number,createtime')->order('id desc')->select();
        $records=collection($records)->toArray();
        foreach ($records as $k=>$v){
            $product_id=intval($v['product_id']);
            $v['parts']=\addons\qingdongams\model\Parts::where('', 'exp', Db::raw('FIND_IN_SET(' .$product_id. ',product_id)'))->select();
            $records[$k]=$v;
        }
        $this->success('请求成功', $records);
    }

    //添加客户产品
    public function addProduct()
    {
        $params = $this->request->post();

        // 表单验证
        if (($result = $this->qingdongamsValidate($params,get_class(), 'create')) !== true) {
            $this->error($result);
        }


        Db::startTrans();
        try {
            $result = CustomerProductModel::createProduct($params);

            Db::commit();
        } catch (Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }
        if ($result) {
            $this->success('创建成功');
        }
    }


    //编辑客户产品
    public function editProduct()
    {
        $id = input('id');
        $params = $this->request->post();
        $row = CustomerProductModel::where(['id' => $id])->find();
        if (empty($row)) {
            $this->error('客户产品不存在');
        }
        // 表单验证
        if (($result = $this->qingdongamsValidate($params,get_class(), 'edit')) !== true) {
            $this->error($result);
        }

        Db::startTrans();
        try {
            $result = CustomerProductModel::updateProduct($params);
            Db::commit();
        } catch (Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }
        if ($result) {
            $this->success('编辑成功');
        }
    }

    //获取客户产品详情
    public function getProductDetail()
    {
        $id = input('id');
        $detail = CustomerProductModel::where(['id' => $id])->with(['createStaff','customer','product'])->find();
        if (empty($detail)) {
            $this->error('产品不存在');
        }

        $this->success('请求成功',$detail);
    }
}
